package main

import (
	"fmt"
	"github.com/urfave/cli"
	"os"
	"terminalpushdemo/src/service/net"
	"terminalpushdemo/src/service/redis"
	"terminalpushdemo/src/service/zookeeper"
	"time"
)

var Version = "0.0.1"

func main() {
	app := cli.NewApp()
	app.Name = "terminal-push-businessgateway"
	app.Version = Version
	app.Compiled = time.Now()
	app.Copyright = "(c) 2018 sudiyi"
	app.Usage = "is ..."
	//commands
	//app.Commands = []cli.Command{
	//	cmdStart,
	//	cmdStop,
	//	cmdReload,
	//}
	//action
	app.Action = func(c *cli.Context) error {
		cli.ShowAppHelp(c)
		c.App.Setup()
		return nil
	}
	// ignore error so we don't exit non-zero and break gfmrun README example tests
	_ = app.Run(os.Args)
	//初始化redis连接池
	redis.Pool = redis.NewPool(*redis.RedisServer, *redis.RedisPassword)
	//实时监听zk配置 和 服务器信息
	conn1 := zookeeper.Connect()
	defer conn1.Close()
	configs, errors := zookeeper.Watch(conn1, "/config")
	go func() {
		for {
			select {
			case config := <-configs:
				fmt.Printf("配置列表：%+v\n", config)
			case err := <-errors:
				fmt.Printf("%+v\n", err)
			}
		}
	}()
	servers, errors := zookeeper.Watch(conn1, "/server")
	go func() {
		for {
			select {
			case server := <-servers:
				fmt.Printf("服务器列表：%+v\n", server)
			case err := <-errors:
				fmt.Printf("%+v\n", err)
			}
		}
	}()

	//创建服务监听
	server := net.Server{}
	server.Start()
}
